#include "sharefile.h"
#include <QCheckBox>
#include <QDebug>
#include "tcpclient.h"
#include "operatewidget.h"
#include "process.h"

ShareFile::ShareFile(QWidget *parent)
    : QWidget{parent}
{
    m_select_all_pb = new QPushButton("全选");
    m_cancel_select_pb = new QPushButton("取消选择");
    m_ok_pb = new QPushButton("确认");
    m_cancel_pb = new QPushButton("取消");

    m_scroll_area = new QScrollArea;
    m_friend_w = new QWidget;
    m_button_group = new QButtonGroup(m_friend_w);
    m_button_group->setExclusive(false);
    m_friend_w_vbl = new QVBoxLayout(m_friend_w); //调用this->test()函数时, 需注释这个布局

    QHBoxLayout* top_hbl = new QHBoxLayout;
    top_hbl->addWidget(m_select_all_pb);
    top_hbl->addWidget(m_cancel_select_pb);
    top_hbl->addStretch();

    QHBoxLayout* down_hbl = new QHBoxLayout;
    down_hbl->addWidget(m_ok_pb);
    down_hbl->addWidget(m_cancel_pb);

    QVBoxLayout* main_vbl = new QVBoxLayout;
    main_vbl->addLayout(top_hbl);
    main_vbl->addWidget(m_scroll_area);
    main_vbl->addLayout(down_hbl);

    setLayout(main_vbl);

    this->test();

    connect(m_cancel_select_pb, &QPushButton::clicked, this, [&]() {
        QList<QAbstractButton*> cb_list = m_button_group->buttons();
        for (int i = 0; i < cb_list.size(); i++) {
            if (cb_list[i]->isChecked()) {
                cb_list[i]->setChecked(false);
            }
        }
    });

    connect(m_select_all_pb, &QPushButton::clicked, this, [&]() {
        QList<QAbstractButton*> cb_list = m_button_group->buttons();
        for (int i = 0; i < cb_list.size(); i++) {
            if (!cb_list[i]->isChecked() ) {
                cb_list[i]->setChecked(true);
            }
        }
    });

    //发送PDU
    connect(m_ok_pb, &QPushButton::clicked, this, [&]() {
        //发送者, 接收者(多个), 文件路径, 文件名
        QString send_name = TcpClient::getInstance().get_login_name();
        QString file_path = TcpClient::getInstance().get_current_dir();
        QString file_name = OperateWidget::getInstance().get_book()->shar_file_name();
        QString complete_path = file_path + "/" + file_name;
        // qDebug() << complete_path;

        int cnt = 0;
        QList<QAbstractButton*> cb_list = m_button_group->buttons();
        QVector<QString> vec_name;
        for (int i = 0; i < cb_list.size(); i++) {
            if (cb_list[i]->isChecked() ) {
                cnt++;
                // qDebug() << cb_list[i]->text();
                vec_name.push_back(cb_list[i]->text());
            }
        }

        PDU* pdu = makePDU(32*cnt + complete_path.size() + 1);
        pdu->uiMsgType = ENUM_MSG_TYPE_SHARE_FILE_REQUEST;
        sprintf(pdu->caData, "%s %d", send_name.toStdString().c_str(), cnt);

        int i = 0;
        for (auto& str : vec_name) {
            // qDebug() << str;
            memcpy((char*)(pdu->caMsg)+i*32, str.toStdString().c_str(), str.size());
            i++;
        }

        memcpy((char*)(pdu->caMsg)+cnt*32, complete_path.toStdString().c_str(), complete_path.size());

        pdu->printPDU();
        TcpClient::getInstance().getTcpSocket().write((char*)pdu, pdu->uiPDULen);
        free(pdu); pdu = NULL;
    });

    //关闭好友选择对话框
    connect(m_cancel_pb, &QPushButton::clicked, this, [&]() {
        QAbstractButton* tmp = NULL;
        QList<QAbstractButton*> p_friend_list = m_button_group->buttons();
        for (int i = 0; i < p_friend_list.size(); i++) {
            tmp = p_friend_list[i];
            m_friend_w_vbl->removeWidget(tmp);
            m_button_group->removeButton(tmp);
            p_friend_list.removeOne(tmp);
            delete tmp; tmp = NULL;
        }
        this->hide();
    });

}

ShareFile &ShareFile::getInstance()
{
    static ShareFile instance;
    return instance;
}

void ShareFile::update_friend(QListWidget *friend_list)
{
#if 1
    if (!friend_list) return;
    QAbstractButton* tmp = NULL;
    QList<QAbstractButton*> p_friend_list = m_button_group->buttons();
    for (int i = 0; i < p_friend_list.size(); i++) {
        tmp = p_friend_list[i];
        m_friend_w_vbl->removeWidget(tmp);
        m_button_group->removeButton(tmp);
        p_friend_list.removeOne(tmp);
        delete tmp; tmp = NULL;
    }

#if 1
    QCheckBox* p_cb = NULL;
    for (int i = 0; i < friend_list->count(); i++) {
        p_cb = new QCheckBox(friend_list->item(i)->text());
        m_friend_w_vbl->addWidget(p_cb);
        //每个复选框也被添加到按钮组 m_button_group 中，方便后续统一管理。
        m_button_group->addButton(p_cb);
        qDebug() << friend_list->item(i)->text();
    }
    // qDebug() << "-------------------------";
    /*
setWidget(m_friend_w) 将 m_friend_w 设置为滚动区域的内容容器，
当内容超出可视范围时会自动显示滚动条。
*/
    m_scroll_area->setWidget(m_friend_w);
#endif

#endif
}

void ShareFile::test()
{
#if 0
    QVBoxLayout* p = new QVBoxLayout(m_friend_w);
    QCheckBox* p_cb = NULL;
    for (int i = 0; i < 25; i++) {
        p_cb = new QCheckBox("jack");
        p->addWidget(p_cb);
        //每个复选框也被添加到按钮组 m_button_group 中，方便后续统一管理。
        m_button_group->addButton(p_cb);
    }
    /*
setWidget(m_friend_w) 将 m_friend_w 设置为滚动区域的内容容器，
当内容超出可视范围时会自动显示滚动条。
*/
    m_scroll_area->setWidget(m_friend_w);
#endif
}


